package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "C端-购物车接口")
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //1：添加购物车
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    @CacheEvict(value = "shoppingCardCache" , allEntries = true)//删除所有缓存信息
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车:{}",shoppingCartDTO.toString());
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    //2：查看购物车
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

    //3：清空购物车
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        log.info("清空购物车...");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
    //4：删除购物车中的一个商品
    @ApiOperation("删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result deleteShoppingOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中的一个商品:{}",shoppingCartDTO.getDishFlavor());
        shoppingCartService.deleteShoppingOne(shoppingCartDTO);
        return Result.success();
    }
}
